package com.gitee.andy.zpush.center;

import java.util.concurrent.ConcurrentLinkedQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gitee.andy.zpush.kernel.Session;
import com.gitee.andy.zpush.kernel.SessionContext;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class CenterClientHandler extends ChannelInboundHandlerAdapter {

	private static final Logger log = LoggerFactory.getLogger(CenterClientHandler.class);
	
	private final SessionContext sessionContext;
	private final ConcurrentLinkedQueue<Object> msgQueue;

	public CenterClientHandler(SessionContext sessionContext, ConcurrentLinkedQueue<Object> msgQueue) {
		super();
		this.sessionContext = sessionContext;
		this.msgQueue = msgQueue;
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		sessionContext.remove(Session.unbind(ctx));
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) {
		msgQueue.offer(msg);
		log.info("receive msg:{}", msg);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
	}
}